﻿using AutoMapper;
using B.S.Backtrack.Domain.Entities;
using B.S.Backtrack.ErrorCode;
using B.S.Backtrack.Infrastructure.Interface;
using B.S.Backtrack.Write.API.Application.Command.Product;
using MediatR;

namespace B.S.Backtrack.Write.API.Application.CommandHandler.Product
{
    public class CreateProductHandler : IRequestHandler<CreateProductCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Products> proRep;
        private readonly IMapper mapper;

        public CreateProductHandler(IBaseRepository<Products> proRep,IMapper mapper)
        {
            this.proRep = proRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model = mapper.Map<Products>(request);
            model.IsDel = false;
            proRep.Add(model);
            result.Code=ApiEnums.添加成功;
            result.Msg=ApiEnums.添加成功.ToString();
            result.Data = 1;
            return Task.FromResult(result);
        }
    }
}
